package com.javasm.mb_store_api_final.giftsandgoods.gift.controller;

import com.javasm.common.R;
import com.javasm.error.ExceptionEnum;
import com.javasm.error.JavasmException;
import com.javasm.mb_store_api_final.giftsandgoods.gift.entity.GmGift;
import com.javasm.mb_store_api_final.giftsandgoods.gift.service.GmGiftService;
import com.javasm.mb_store_api_final.giftsandgoods.gift.vo.GiftQueryVo;
import com.javasm.mb_store_api_final.giftsandgoods.gift.vo.GiftVo;
import com.javasm.mb_store_api_final.giftsandgoods.goods.entity.GgGoods;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @className:GiftController
 * @author: zhl
 * @date:2024/7/2915:48
 * @since : jdk11
 */
@RestController
@RequestMapping("/gift")
public class GiftController {
    @Resource
    GmGiftService gmGiftService;
    @GetMapping("/listGifts")
    public R listGifts() {
       List<GiftVo> giftVos = gmGiftService.listGifts();
        return R.ok(giftVos);
    }
    @GetMapping("/getTatol")
    public R getTatol() {
        long count =  gmGiftService.count();
        return R.ok(count);
    }
    @GetMapping("/getGift/{id}")
    public R getGift(@PathVariable Integer id) {
        GmGift gift = gmGiftService.getById(id);
        return R.ok(gift);
    }

    @GetMapping("/getGiftDetail/{id}")
    public R getGiftDetail(@PathVariable Integer id) {
        GgGoods goods = gmGiftService.listDetail(id);
        return R.ok(goods);
    }
    @GetMapping("/paramList")
    public R paramList() {
        Map<String, List<String>> map = gmGiftService.paramList();
        return R.ok(map);
    }
    @PostMapping("/ListByParams")
    public R ListByParams(@RequestBody GiftQueryVo giftQueryVo) {
        List<GiftVo> giftVos=gmGiftService.ListByParams(giftQueryVo);
        return R.ok(giftVos);
    }
    @PostMapping("/updateGift")
    public R updateGift(@RequestBody GiftVo giftVo) {

       Integer i = gmGiftService.updateGift(giftVo);
        if (i==2){
            return R.ok();
        }else{
            throw new JavasmException(ExceptionEnum.UpdateError);
        }

    }
    @DeleteMapping("/deleteGift/{id}")
    public R deleteGift( @PathVariable Integer id) {
        boolean b = gmGiftService.removeById(id);
        if (b){
            return R.ok();
        }else{
            throw new JavasmException(ExceptionEnum.DeleteError);
        }
    }
}
